<?
class sym_user extends symbiont{
    public function ___main($design=''){
        global $user;
        if(!$design) $design='main';
        if($user->access_level){
            $design.='/signout';
            $this->___signout($design);
        }
        else{
            $design.='/signin';
            $this->___signin($design);
        }
    }
    public function ___signin($design=''){
        global $user;
        $design=$this->_check($design, 'signin');
        $this->un_key=$user->un_key;
        $d=new design($design, 'user');
    }
    public function ___signout($design=''){
        global $user;
        $design=$this->_check($design, 'main/signout');
        $this->user=$user->login;
        $d=new design($design, 'user');
    }
    public function ___signup($design=''){
        global $user;
        $l=new label('symbionts.user.login');
        $this->message_login=$l->get();
        $this->un_key=$user->un_key;
        $design=$this->_check($design, 'signup');
        $d=new design($design, 'user');
    }
    public function ___post_signin($design=''){
        global $user;
        if(isset($_POST['login'])&&isset($_POST['password'])){
            $signin=$user->signin($_POST['login'], $_POST['password']);
            if($signin){
                return '{"ok":""}';
            }
            else{
                $l=new label('symbionts.user.signin_error');
                return '{"error":"'.$l->get().'"}';
            }
        }
        else{
            return '{"error":"Not the all parametrs have been sent."}';
        }
    }
    public function ___post_signout($design=''){
        global $user;
        $user->signout();
    }
    public function ___db_signup($design=''){
        global $sql;
        if(!isset($_POST['login'])||!isset($_POST['pass'])||!isset($_POST['email'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $login=data::word($_POST['login']);
        $pass=data::word($_POST['pass']);
        if(!data::is_email($_POST['email'])){
            $l=new label('symbionts.user.email_error');
            print('{"error":"'.$l->get().'"}');
            return;
        }
        $email=$_POST['email'];
        if($sql->count('users', array('email'=>$email))){
            $l=new label('symbionts.user.email_exists_error');
            print('{"error":"'.$l->get().'"}');
            return;
        }
        
        $firstname=isset($_POST['firstname'])?data::safe($_POST['firstname']):'';
        $lastname=isset($_POST['lastname'])?data::safe($_POST['lastname']):'';
        $city=isset($_POST['city'])?data::safe($_POST['city']):'';
        $country=isset($_POST['country'])?data::safe($_POST['country']):'';
        $phone=isset($_POST['phone'])?data::safe($_POST['phone']):'';
        $address=isset($_POST['address'])?data::safe($_POST['address']):'';
        if($firstname&&$lastname&&$city&&$country&&$phone&&$address){
            $access_level=2;
        }
        else{
            $access_level=1;
        }
        $id=$sql->insert('users', array('login'=>$login, 'password'=>$pass, 'email'=>$email, 'firstname'=>$firstname, 'lastname'=>$lastname, 'city'=>$city, 'country'=>$country, 'phone'=>$phone, 'address'=>$address, 'access_level'=>$access_level));
        $signin=isset($_POST['signin'])&&$_POST['signin']?true:false;
        if($id){
            if($signin){
                $this->___post_signin('');
            }
            $l=new label('symbionts.user.signup_complete');
            return '{"ok":"'.$l->get().'"}';
        }
        else{
            $l=new label('symbionts.user.signup_error');
            return '{"error":"'.$l->get().'"}';
        }
    }
    public function ___check($design, $login=''){
        global $sql;
        if(isset($_POST['login'])){
            $login=data::word($_POST['login'], 'en', 0, '-');
        }
        else{
            $login=data::word($login, 'en', 0, '-');
        }
        $count=$sql->count('users', array('login'=>$login));
        if($count){
            $l=new label('symbionts.user.login_error');
            return '{"error":"'.$l->get().'"}';
        }
        else{
            $l=new label('symbionts.user.login_ok');
            return '{"ok":"'.$l->get().'"}';
        }
    }
    public function ___profile($design=''){
        global $sql, $user;
        if($design) $design='profile/'.$design;
        $design=$this->_check($design, 'profile/edit');
        $d=new design($design, 'user', false);
        
        $u=$sql->select_each('users', array('firstname', 'lastname', 'email', 'country', 'city', 'address', 'phone'), array('id'=>$user->id), '', '', 1);
        foreach($u as $key=>$val){
            $this->$key=$val;
        }
        $d->inc();
    }public function ___db_profile($design=''){
        global $sql, $user;
        
        $firstname=isset($_POST['firstname'])?data::safe($_POST['firstname']):'';
        $lastname=isset($_POST['lastname'])?data::safe($_POST['lastname']):'';
        $city=isset($_POST['city'])?data::safe($_POST['city']):'';
        $country=isset($_POST['country'])?data::safe($_POST['country']):'';
        $phone=isset($_POST['phone'])?data::safe($_POST['phone']):'';
        $address=isset($_POST['address'])?data::safe($_POST['address']):'';
        
        if($firstname&&$lastname&&$city&&$country&&$phone&&$address){
            $access_level=2;
        }
        else{
            $access_level=1;
        }
        
        $id=$sql->update('users', array('firstname'=>$firstname, 'lastname'=>$lastname, 'city'=>$city, 'country'=>$country, 'phone'=>$phone, 'address'=>$address, 'access_level'=>$access_level), array('id'=>$user->id));
        if($id){
            $l=new label('symbionts.user.profile_saved');
            return '{"ok":"'.$l->get().'"}';
        }
        else{
            $l=new label('symbionts.user.profile_error');
            return '{"error":"'.$l->get().'"}';
        }
    }
    public function _administrate($design=''){
        $design=$this->_check($design, 'administrate');
        $d=new design($design, 'user', false);
        $this->name='signin';
        $this->title='Sign In';
        $this->options=design::run($this->option);
        $this->name='signup';
        $this->title='Sign Up';
        $this->options.=design::run($this->option);
        $d->inc();
    }
}
?>